<?php
require_once("includes/init.php");

#Gather data
timeTimerStart("Gathering data from database... ");
$observers = databaseGetObservers();
$ads = databaseGetNewAdsFromYesterday();
$allCategories = databaseGetAllCategories();
$categoryAds = databaseGetCategoryAds();
timeTimerStop();

timeTimerStart("Extending categoryAds array from sub categories... ");
$categoryAds = categoryGetAllCategoryAds($allCategories,$categoryAds);
timeTimerStop();


timeTimerStart("Checking user subscriptions... \n");
foreach ($observers as $observer=>$subscriptions) {
	$matches = array();
	
	if (is_array($subscriptions))
		foreach($subscriptions as $category=>$keywords) {
			if ($globalDebug)
				print "Looking in category $category for ".count($keywords)." words.\n";
			
			if ($globalDebug)
				print "Checking category $category for ads\n";
			
			if (is_array($categoryAds[$category]))
				foreach($categoryAds[$category] as $ad_id) {
					if ($globalDebug)
						print "Searching ad $ad_id\n";
					
					if (searchSearch($keywords,$ads[$ad_id])) {
						$matches[$ad_id] = $ad_id;
						print "+";
					} else {
						print "-";
					}
				}
		}
	
	if (count($matches) > 0)
		emailEmail($observer, $matches, $ads);
	
	print "\n";
}

timeTimerStop();
?>
